package com.kingzoon.seckill.web;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import java.util.HashMap;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.kingzoon.seckill.base.login.LoginHelper;
import com.kingzoon.seckill.dto.Result;
import com.kingzoon.seckill.enums.MacroDefine;
import com.kingzoon.seckill.service.impl.base.PasswordHelper;





@Controller
@Api(value = "登录接口")
public class MemberLoginController {
	
	
	@Autowired
	private PasswordHelper passwordHelper;
	
	@ApiOperation(value = "用户登录")
	@RequestMapping(value="/login",method=RequestMethod.POST,produces={"application/json;charset=utf-8"})
	@ResponseBody
	public Result<HashMap<String,Object>> login(
			@ApiParam(required = true, value = "登录帐号")@RequestParam("account")String account,
			@ApiParam(required = true, value = "登录密码")@RequestParam("password")String password){
		Result<HashMap<String,Object>> rs = new Result<HashMap<String,Object>>(false, MacroDefine.JC_CODE.R_9999.getCode(), MacroDefine.JC_CODE.R_9999.getMessage());
		if(LoginHelper.Login(account, passwordHelper.encryptPassword(password))){
			rs.setFlag(true);
			rs.setCode(MacroDefine.JC_CODE.S_0000.getCode());
			rs.setMessage(MacroDefine.JC_CODE.S_0000.getMessage());
		}
		return rs;
	}
	
	// 登出
	@ApiOperation(value = "用户登出")
	@RequestMapping(value="/logout",method=RequestMethod.POST,produces={"application/json;charset=utf-8"})
	@ResponseBody
	public Result<HashMap<String,Object>> logout(ModelMap modelMap) {
		Result<HashMap<String,Object>> rs = new Result<HashMap<String,Object>>(false, MacroDefine.JC_CODE.R_9999.getCode(), MacroDefine.JC_CODE.R_9999.getMessage());
		SecurityUtils.getSubject().logout();
		rs.setFlag(true);
		rs.setCode(MacroDefine.JC_CODE.S_0000.getCode());
		rs.setMessage(MacroDefine.JC_CODE.S_0000.getMessage());
		return rs;
	}

}
